using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WMS.Api.Application.Commands;
using WMS.Common;

namespace WMS.Api.Controllers
{
    /// <summary>
    /// 入库控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class InboundController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<InboundController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public InboundController(IMediator mediator, ILogger<InboundController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        #region 入库类型

        /// <summary>
        /// 获取入库类型列表
        /// </summary>
        /// <returns>入库类型列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<InboundTypeDto>>> GetInboundTypeList()
        {
            try
            {
                var command = new GetInboundTypeListCommand();
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取入库类型列表失败");
                return new ApiResult<List<InboundTypeDto>>("获取入库类型列表失败：" + ex.Message);
            }
        }

        #endregion

        #region 入库单

        /// <summary>
        /// 获取入库单列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="warehouseId">仓库ID</param>
        /// <param name="inboundTypeId">入库类型ID</param>
        /// <param name="referenceNo">参考号</param>
        /// <returns>入库单列表</returns>
        [HttpGet]
        public async Task<ApiResult<PagingResult<List<InboundOrderDto>>>> GetInboundOrderList(
            int pageIndex = 1,
            int pageSize = 10,
            int? warehouseId = null,
            int? inboundTypeId = null,
            string? referenceNo = null)
        {
            try
            {
                var command = new GetInboundOrderListCommand
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    WarehouseId = warehouseId,
                    InboundTypeId = inboundTypeId,
                    ReferenceNo = referenceNo
                };

                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取入库单列表失败");
                return new ApiResult<PagingResult<List<InboundOrderDto>>>("获取入库单列表失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 获取入库单详情
        /// </summary>
        /// <param name="id">入库单ID</param>
        /// <returns>入库单详情</returns>
        [HttpGet]
        public async Task<ApiResult<InboundOrderDetailDto>> GetInboundOrderById(int id)
        {
            try
            {
                var command = new GetInboundOrderByIdCommand { Id = id };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取入库单详情失败");
                return new ApiResult<InboundOrderDetailDto>("获取入库单详情失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 创建入库单
        /// </summary>
        /// <param name="inboundOrder">入库单信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateInboundOrder([FromBody] InboundOrderCreateDto inboundOrder)
        {
            try
            {
                var command = new CreateInboundOrderCommand { InboundOrder = inboundOrder };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "创建入库单失败");
                return new ApiResult<int>("创建入库单失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 更新入库单
        /// </summary>
        /// <param name="inboundOrder">入库单信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateInboundOrder([FromBody] InboundOrderUpdateDto inboundOrder)
        {
            try
            {
                var command = new UpdateInboundOrderCommand { InboundOrder = inboundOrder };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "更新入库单失败");
                return new ApiResult<int>("更新入库单失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 删除入库单
        /// </summary>
        /// <param name="id">入库单ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DeleteInboundOrder(int id)
        {
            try
            {
                var command = new DeleteInboundOrderCommand { Id = id };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除入库单失败");
                return new ApiResult<int>("删除入库单失败：" + ex.Message);
            }
        }

        /// <summary>
        /// 上传入库单附件
        /// </summary>
        /// <param name="inboundOrderId">入库单ID</param>
        /// <param name="attachmentType">附件类型</param>
        /// <param name="file">文件</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<ApiResult<string>> UploadInboundAttachment(int inboundOrderId, string attachmentType, IFormFile file)
        {
            try
            {
                var command = new UploadInboundAttachmentCommand
                {
                    InboundOrderId = inboundOrderId,
                    AttachmentType = attachmentType,
                    File = file
                };
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "上传入库单附件失败");
                return new ApiResult<string>("上传入库单附件失败：" + ex.Message);
            }
        }

        #endregion
    }
} 